HES 505 Fall 2022: Session 15
Update on assignments
Refresher: Building a spatial analysis workflow
Building a database for an analysis (part 2) based on location
Assignment 2 due by 14 Oct
Self-assessment 2 due 21 Oct
Resubmits
Final Project
By the end of today you should be able to:
Create new features based on topological relationships
Use topological subsetting to reduce features
Use spatial joins to add attributes based on location
“The process of examining the locations, attributes, and relationships of features in spatial data through overlay and other analytical techniques in order to address a question or gain useful knowledge. Spatial analysis extracts or creates new information from spatial data”.
Align processing with objectives
Imagining the visualizations and analysis clarifies file formats and variables
Helps build reproducibility
Attributes: Information that further describes a spatial feature
Attributes → predictors for analysis
Last week focus on thematic relations between datasets
Sometimes we are interested in attributes that describe location (overlaps, contains, distance)
Sometimes we want to join based on location rather than thematic connections
measures)Attributes like area and length can be useful for a number of analyses
Need to assign the result of the function to a column in data frame (e.g., $, mutate, and summarize)
Often useful to test before assigning
Creating new features based on the frequency of occurrence
Clarifying graphics
Underlies quadrat sampling for point patterns
Two steps: count and area
As a covariate
For use in covariance matrices
As a means of assigning connections in networks
st_distance returns distances between all features in x and all features in y
One-to-One relationship requires choosing a single point for y
y into a single featureTopological relations describe the spatial relationships between objects
We can use the overlap (or not) of vector data to subset the data based on topology
Need valid geometries
Easiest way is to use [ notation, but also most restrictive
Lots of verbs in sf for doing this (e.g., st_intersects, st_contains, st_touches)
see ?geos_binary_pred for a full list
Creates an implicit attribute (the records in x that are “in” y)
sparse option controls how the results are returnedUsing sparse=FALSE
sf package provides st_join for vectors
Allows joins based on the predicates (st_intersects, st_touches, st_within_distance, etc.)
Default is a left join
set.seed(2018)
(bb = st_bbox(world)) # the world's bounds
#> xmin ymin xmax ymax
#> -180.0 -89.9 180.0 83.6
random_df = data.frame(
x = runif(n = 10, min = bb[1], max = bb[3]),
y = runif(n = 10, min = bb[2], max = bb[4])
)
random_points = random_df |>
st_as_sf(coords = c("x", "y")) |> # set coordinates
st_set_crs("EPSG:4326") # set geographic CRS
random_joined = st_join(random_points, world["name_long"])Sometimes we may want to be less restrictive
Just because objects don’t touch doesn’t mean they don’t relate to each other
Can use predicates in st_join
Remember that default is left_join (so the number of records can grow if multiple matches)
st_intersection, st_union, and st_difference return new geometries that we can use as records in our spatial databaseintersect_pct <- st_intersection(nc, tr_buff) %>%
mutate(intersect_area = st_area(.)) %>% # create new column with shape area
dplyr::select(NAME, intersect_area) %>% # only select columns needed to merge
st_drop_geometry()
nc <- mutate(nc, county_area = st_area(nc))
# Merge by county name
nc <- merge(nc, intersect_pct, by = "NAME", all.x = TRUE)
# Calculate coverage
nc <- nc %>%
mutate(coverage = as.numeric(intersect_area/county_area))